/**
 * related to clientInfo.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var nf = sm("do_Notification");
var app = sm("do_App");
var page = sm("do_Page");
var Controller = require("core/controller");
var log = require("util/log")
var navigate = require("util/navigate");
var net = require("util/net");
var server = require('config/server')
var router = require("config/router")
var navigate = require("util/navigate")
var el_Client = ui("el_Client");
var btn_repayInfo = ui("btn_repayInfo");
var btn_repayApply = ui("btn_repayApply");
var filter = require('util/filter')
var common = require('util/common')
var promise = require('lib/promise')
Controller({
	name: 'index',
	mapping: {
		"el_Client": ["clientTitle", "clientContent"]
	},
	data: {
		custId: '', // 客户ID
		coupId: '', // 案件ID
		cupeDate: '', // 案件创建日期
		addressList: [], // 客户地址信息列表
		contactList: [], // 客户联系人信息列表
		JobList: [], // 客户单位信息列表
		clientBase: {}, // 客户基本信息
		contantList:[],
		// 客户列表头部信息
		clientTitle: [
			/**
			 * 给数组模板添加数据source://view/do_ExpandableListView/test2.ui
			 */
			{
				template: 0,
				custName: "",
				sex: "",
				age: "",
				idCardNumber: "",
				custMobileStatus: "",
				custMobile: "",
				cupoGettime: ""
			}, {
				template: 1,
				"client_titleText": "地址信息",
				"cl_icon": "e6ac"
			}, {
				template: 1,
				"client_titleText": "联系人信息",
				"cl_icon": "e6ac"
			}, {
				template: 1,
				"client_titleText": "单位信息",
				"cl_icon": "e6ac"
			},
		],
		// 客户列表内容信息
		clientContent: [
			/**
			 * 给数组模板添加数据
			 */
			[{
				template: 0,
			}],
			[{
				template: 1,
				"relation": "",
				"custName": "",
				"addressType": "",
				"updateTime": "",
				"addressStatus": "",
				"detailAddress": ""
			}],
			[{
				template: 2,
				"relation": "",
				"contactName": "",
				"contactPhoneStatus": "",
				"contactPhone": "",
				"updateTime": ""
			}],
			[{
				template: 3,
				"employer": "",
				"department": "",
				"rank": "",
				"companyPhone": ""
			}]
		]
	},
	ui: {
		root: '$'
	},
	event: {
		'btn_repayInfo': {
			'touch': function () {
				this.openRepoInfo(this.data.custId)
			}
		},
		'btn_repayApply': {
			'touch': function () {
				this.openRepoApply(this.data.cupoId)
				log.info(333,this.data)
			}
		},
		'el_Client': {
			"groupExpand": function (index) {
				el_Client.collapseGroup([1, 2, 3].filter(function (x) {
					return x !== index
				}))
				var data = this.data.clientTitle[index]
				data.cl_icon = "e6aa"
				this.getSource('el_Client')[0].updateOne(index, data)
				// el_Client.redraw()
			},
			"groupCollapse": function (index) {
				var data = this.data.clientTitle[index]
				data.cl_icon = "e6ac"
				this.getSource('el_Client')[0].updateOne(index, data)
			}
		}
	},
	methods: {
		/**
		 * 打开修复信息页面
		 */
		openRepoInfo: function (da) {
			navigate.redirectTo({
				view: router.client.repairInfo,
				data: da,
				back: function (data) {
					this.getClientData()
				},
				scope: this
			})
		},
		/**
		 * 打开修复申请页面
		 */
		openRepoApply: function (aply) {
			log.info(9999999, aply)
			navigate.redirectTo({
				view: router.client.repairApply,
				data: aply,
				back: function (data) {
					232323
				}
			})
		},
		/**
		 * 获取用户地址信息
		 */
		getAddressData: function () {
			return net.send({
				server: server.organization.requireClientAddress,
				data: {
					custId: this.data.custId
				},
				success: function (data) {
					// 获取处理后的地址信息
					log.info(544,data)
					this.data.addressList = data.map(function (x) {
						if (x.flag) {
							// 原始信息处理业务
							return {
								relation: filter.typeCodeConvert(x.relation),
								custName: filter.typeCodeConvert(x.name),
								addressType: filter.typeCodeConvert(x.addressType),
								updateTime: common.dateFormat(x.updateTime, 'yyyy-MM-dd'),
								addressStatus: filter.typeCodeConvert(x.addressStatus),
								detailAddress: x.detailAddress||'',
								addressID: {custName:filter.typeCodeConvert(x.name),mapInfo: x.mapInfo,detailAddress:x.detailAddress||''},
								template: 1
							}
						} else {
							var detailAddress = filter.getCityName(x.provinceId) + filter.getCityName(x.cityId) + filter.getCityName(x.countyId) + x.address
							// 修复信息处理业务
							return {
								relation: filter.typeCodeConvert(x.relation),
								custName: filter.typeCodeConvert(x.name),
								addressType: filter.typeCodeConvert(x.addressType),
								updateTime: common.dateFormat(x.date, 'yyyy-MM-dd'),
								addressStatus: filter.typeCodeConvert(x.addressStatus),
								detailAddress: detailAddress,
								addressID: {custName:filter.typeCodeConvert(x.name),mapInfo: x.mapInfo,detailAddress:detailAddress},
								template: 1
							}
						}
					})
				},
				error: function (data) {
					log.info(data)
				},
				scope: this
			})
		},
		/**
		 * 获取用户联系人信息
		 */
		getContactsDetail:function(){
			return net.send({
				server: server.organization.requireClientContant,
				data: {
					custId: this.data.custId
				},
				success: function (x) {		
					log.info(141,x)
					this.data.contactList=x.map(function (x) {		
							if (x.flag) {
								return {
									relation: filter.typeCodeConvert(x.relation),
									contactName: x.contactName || '',
									contactPhoneStatus: filter.typeCodeConvert(x.contactPhoneStatus),
									contactPhone: filter.encryptPhone(x.contactPhone),
									updateTime: common.dateFormat(x.date, 'yyyy-MM-dd'),
									telIndex: x.contactPhone || '',
									template: 2
								}
							} else {
								return {
									relation: filter.typeCodeConvert(x.relation),
									contactName: x.contactName || '',
									contactPhoneStatus: filter.typeCodeConvert(x.contactPhoneStatus),
									contactPhone: filter.encryptPhone(x.contactPhone),
									updateTime: common.dateFormat(x.updateTime, 'yyyy-MM-dd'),
									telIndex: x.contactPhone || '',
									template: 2
								}
							}
							
						})	
				},
				error: function (data) {
					log.info(433,data)
				},
				scope: this
			})
		},
		/**
		 * 获取客户详细信息
		 */
		getClientDetail: function () {
			return net.send({
				server: server.organization.getClientInfoDetails,
				append: this.data.custId,
				success: function (data) {
					log.info(data)
					// 获取联系人列表
//					this.data.contactList = data.customerContacts.map(function (x) {
//						return {
//							relation: filter.typeCodeConvert(x.relation),
//							contactName: x.contactName || '',
//							contactPhoneStatus: filter.typeCodeConvert(x.contactPhoneStatus),
//							contactPhone: filter.encryptPhone(x.contactPhone),
//							updateTime: common.dateFormat(x.updateTime, 'yyyy-MM-dd'),
//							telIndex: x.contactPhone || '',
//							template: 2
//						}
//					})
					// 获取单位信息列表
					this.data.JobList = [{
						employer: data.customerJobInformation.employer || '',
						department: data.customerJobInformation.department || '',
						rank: data.customerJobInformation.rank || '',
						companyPhone: data.customerJobInformation.companyPhone || '',
						bottomTelID: data.customerJobInformation.companyPhone || '',
						template: 3
					}]
					// 获取客户基本信息
					this.data.clientBase = {
						custName: data.custName || '',
						sex: data.sex || '',
						age: data.age || '',
						idCardNumber: filter.encryptIdCard(data.idCardNumber),
						custMobileStatus: filter.typeCodeConvert(data.custMobileStatus) || '',
						custMobile: data.custMobile || '',
						cupoGettime: this.data.cupeDate||'',
						toptelId: data.custMobile || '',
						template: 0
					}
				},
				error: function (data) {},
				scope: this
			})
		},
		/**
		 * 获取客户信息页面基础数据
		 */
		getClientData: function () {
			var self = this
			var process = new promise.Promise()
			promise.join([
				this.getAddressData(),
				this.getClientDetail(),
				this.getContactsDetail(),
			]).then(function () {
				try {
					log.info(self)
					log.info(self.data)
					// 更新客户信息
					self.data.clientTitle[0] = self.data.clientBase
					self.data.clientContent[1] = self.data.addressList
					self.data.clientContent[2] = self.data.contactList
					self.data.clientContent[3] = self.data.JobList
					self.refresh('el_Client')
				} catch (ex) {
					log.info(ex)
				}
				process.done()
			})
			return process
		}
	},
	loaded: function (data) {
		this.data.custId = data.current.custId
		this.data.cupoId = data.current.cupoId
		this.data.cupeDate = data.current.cupoGettime
		log.info(112,data.current.custId)
		this.loadReady([this.getClientData()])
	}
})